package cc.plumgo.ss.ui.page;

import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;

import cc.plumgo.j2me.utils.CustomPage;
import cc.plumgo.j2me.utils.StringUtils;
import cc.plumgo.ss.core.Console;
import cc.plumgo.ss.core.Constants;
import cc.plumgo.ss.ui.UIEngine;

public class PostPage extends CustomPage {
	public static final int POST_HIDE = 0;
	public static final int POST_SHOW = 1;
	public static final int POST_STATE_NUM = 2;
	
	private Font font;
	private int color = 0x00000000;
	private int delay = 5;
	public PostPage() {
		super();
	}
	
	public void loadFont() {
		if (font == null) {
			try {
				font = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
			} catch(Exception e) {
				font = Font.getDefaultFont();
			}
		}
	}
	
	public void process(int keyStates, Graphics g) {
		switch(state) {
		case POST_HIDE : {
			Console.getInstance().getGameListener().showPage(UIEngine.UI_MENU);
			break;
		}
		case POST_SHOW : {
			loadSprite(Constants.UI_IMG_POST, Constants.UI_POST_FRM_SIZ[0],
					Constants.UI_POST_FRM_SIZ[1]);
			sprite.setFrame(0);
			paint(g);
			
			g.setFont(font);
			int back = g.getColor(); 
			if (delay == 0) {
				if (color == 0xFFFFFFFF) {
					color = 0x00000000;
				} else {
					color = 0xFFFFFFFF;
				}
			}
			g.setColor(color);
			StringUtils.adjustDrawString(g, Constants.UI_TXT_POST, 80, 40, 200, 10);
			g.setColor(back);
			
			state = (keyStates & GameCanvas.FIRE_PRESSED) != 0 ? POST_HIDE : state;	
			delay = (-- delay < 0)? 5 : delay;
		}
		}
	}
}
